package com.yjd.job.repayment;

import com.alibaba.edas.schedulerX.ProcessResult;
import com.alibaba.edas.schedulerX.ScxSimpleJobContext;
import com.alibaba.edas.schedulerX.ScxSimpleJobProcessor;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.enums.StagesStatusEnum;
import com.yjd.comm.order.model.OrderModel;
import com.yjd.comm.order.service.IOrderService;
import com.yjd.comm.repayment.dto.OverdueDto;
import com.yjd.comm.repayment.model.RepaymentDataModel;
import com.yjd.comm.repayment.service.IRepaymentDataService;
import com.yjd.comm.util.*;
import com.yjd.comm.vo.Constant;
import org.apache.log4j.Logger;

import javax.annotation.Resource;
import java.util.List;

/**
 * 逾期还款提醒
 * Created by yjd-wap on 2017/11/15.
 */
public class RepaymentOverDueReminderJob implements ScxSimpleJobProcessor {
    Logger logger = Logger.getLogger(RepaymentOverDueReminderJob.class);

    @Resource
    private IOrderService orderService;

    @Resource
    private IRepaymentDataService repaymentDataService;


    @Override
    public ProcessResult process(ScxSimpleJobContext scxSimpleJobContext) {
        int page = 1, rows = 10;
        Long currentTime = FrameUtil.getTime();
        logger.info("逾期还款提醒，日期："+currentTime+"开始");
        try {
            while (true) {
                // 查询status（是否已还 0：否，1是 2 取消），应还日期estimation_repayment_time小于当前时间的订单
                List<RepaymentDataModel> reminderList = repaymentDataService.reminderRepaymentOrder(
                        FrameUtil.newHashMap(
                                "page", page, "rows", rows,
                                "status", StagesStatusEnum.REPAYMENTING.getCode(),
                                "estimation_repayment_time_le", currentTime + 1));
                if (null == reminderList || reminderList.size() == 0) {
                    break;
                }

                // 推送还款消息
                OrderModel order = null;
                Double overdue_price = 0D; // 逾期利息
                Double all_price = 0D; // 金额总计
                for (RepaymentDataModel repayment : reminderList) {
                    // 计算罚息金额
                    OverdueDto overdueDto = RepaymentUtils.overdueInterest(repayment.getRepayment_amount(), repayment.getEstimation_repayment_time(), FrameUtil.getTime());
                    overdue_price = overdueDto.getOverduePrice();

                    all_price = ArithUtil.add(repayment.getRepayment_amount(), overdue_price);
                    order = orderService.getModelById(repayment.getOrder_id(), DbWREnums.READ);
                    JpushUtil.pushMessage(
                            order.getUser_id(),
                            Constant.SysParamKeyEnum.PUSH_TPL_OVER_DUE_REMINDER,
                            order.getOrder_no(),
                            order.getLimit_pay() + 1,
                            order.getPeriod(),
                            overdueDto.getOverdueDay(),
                            NumberUtil.formatNumber(repayment.getRepayment_amount(), NumberUtil.FORMAT_4),
                            NumberUtil.formatNumber(overdue_price, NumberUtil.FORMAT_4),
                            NumberUtil.formatNumber(all_price, NumberUtil.FORMAT_4));
                }
                page++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("逾期还款提醒，日期："+currentTime+"结束");
        return new ProcessResult(true);
    }
}
